package com.wc5818.api;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.node.BooleanNode;
import com.wc5818.common.BaseTools;
import com.wc5818.common.SendSms;
import com.wc5818.entity.User;
import com.wc5818.entity.WC_CAR_Info;
import com.wc5818.entity.WC_COM_Order;
import com.wc5818.entity.WC_FK_CustInfo;
import com.wc5818.mapper.WC_CAR_InfoMapper;
import com.wc5818.service.UserService;
import com.wc5818.service.WC_CAR_InfoService;
import com.wc5818.service.WC_COM_OrderService;
import com.wc5818.service.WC_FK_CustInfoService;
import com.wc5818.other.renrunyun.RRY;

/****
 * 
 * 标准 get 函数名 get表示获取数据 add 函数表示添加保存数据 up函数前缀表示修改数据 del函数表示删除数据 按照这个函数起名规范 表明 与
 * 返回类型 大写 All 表示所有集合，实体名 表示单个实体名，Bl 表示布尔 true，false 类型， Str是字符串类型  Int表示 数字类型
 * 类名 表明 WC_模块名大写_表明首字母大写 数据库命令全部小写
 * 
 * 后第一个字母首写字母大写
 * 
 * @author yqb
 *
 */
@RestController
@RequestMapping("/wxapp/api")
public class WxApp {

	/***
	 * 用户管理
	 */
	@Autowired
	private UserService userService;

	/**
	 * 订单5个界面发起
	 */
	@Autowired
	private WC_FK_CustInfoService custInfoService;

	/**
	 * 车辆信息
	 */
	@Autowired
	private WC_CAR_InfoService carinfo;
	/**
	 * 订单表
	 */
	@Autowired
	private WC_COM_OrderService comorder;

	/****
	 * 获取指定的username数据
	 * 
	 * @return
	 */
	@RequestMapping(value = "getRandomForOrderNum", method = RequestMethod.GET)
	public String getRandomForOrderNum() {

		return BaseTools.getRandomForOrderNum();

	}

	/****
	 * 获取指定的username数据
	 * 
	 * @return
	 */
	@RequestMapping(value = "getUserUserName", method = RequestMethod.GET)
	public String getUserUserName(@RequestParam("username") String username) {

		try {
			return userService.sel(username).toString();
		} catch (NullPointerException err) {

			return BaseTools.EMPTY;

		}

	}

	/***
	 * 
	 * @return
	 */
	@RequestMapping("getAllUser")
	public List<User> getAllUser() {
		List<User> list = userService.findAllUser();

		return list;
	}

	/***
	 * 用户登录 根据 username 根据 密码 password
	 * 
	 * @return 返回User 表示登录成功 返回null 表示登录失败
	 */
	@RequestMapping(value = "getUserUserAnPassLogin", method = RequestMethod.GET)
	public User getUserUserAnPassLogin(@RequestParam("username") String username, @RequestParam("pass") String pass) {
		List<User> list = userService.selUserUserAnPassLogin(username, BaseTools.getMD5(pass));
		if (list.size() == 1) {
			return list.get(0);
		}

		return null;
	}

	/***
	 * 持久化数据到 user u
	 * 
	 * 用户注册函数
	 * 注册成功返回 注册对象
	 * 否则返回null
	 * 
	 */
	@RequestMapping(value = "addUser", method = RequestMethod.POST)
	public User addUser(User u) {

		u.setCreatetime(BaseTools.getCurrentTimeMillis());
		u.setPassWord(BaseTools.getMD5(u.getPassWord()));

		if(userService.insert(u) == 1) {
			
			return userService.findAllUser().get(userService.findAllUser().size() - 1);
			
		}
		
		
		return null;
	}

	/***
	 * 发送验证码 设置手机号
	 */
	@RequestMapping(value = "getSendSmsCode", method = RequestMethod.GET)
	public long sendSmsCode(@RequestParam("phone") String phone) {

		return SendSms.sendcode(phone);

	}

	/***
	 * 返回邀请码八位
	 * 
	 * @return
	 */
	@RequestMapping("getInviterCode")
	public long inviterCode() {

		return BaseTools.getRandomNum(8);

	}

	/***
	 * 根据code获取数据是否存在
	 * 
	 * @param code
	 * @return
	 */
	@RequestMapping(value = "getSelForCode", method = RequestMethod.GET)
	public int getSelForCode(@RequestParam("code") String code) {

		if (userService.selForCode(code).size() <= 0) {

			return 0;

		} else {

			return 1;
		}

	}

	
	/****
	 * 根据username获取User对象相关数据
	 * 
	 * @param username
	 * @return
	 */
	@RequestMapping(value = "getUserForUserName", method = RequestMethod.GET)
	public User getUserForUserName(@RequestParam("username") String username) {

		User allUser = userService.sel(username);

		return allUser;

	}
	
	
	/****
	 * 根据Code获取相关数据
	 * 
	 * @param code
	 * @return
	 */
	@RequestMapping(value = "getAllUserCode", method = RequestMethod.GET)
	public List<User> getAllUserCode(@RequestParam("code") String code) {

		List<User> allUser = userService.selAllUserCode(code);

		return allUser;

	}

	// ***********************************************************************************************************************

	/*****
	 * 车辆信息表操作***********************************************************
	 * 
	 */

	/**
	 * 获取车辆表所有数据
	 * 
	 * @return
	 */
	@RequestMapping(value = "getAllWCCARInfo", method = RequestMethod.GET)
	public List<WC_CAR_Info> getAllWCCARInfo() {

		return carinfo.getAllWCCARInfo();
	}

	/**
	 * 根据carid 获取相关所有数据
	 * 
	 * @param carid
	 * @return
	 */
	@RequestMapping(value = "getWCCARInfoCarID", method = RequestMethod.GET)
	public List<WC_CAR_Info> getWCCARInfoCarID(@RequestParam("carid") int carid) {

		return carinfo.getWCCARInfoCarID(carid);
	}

	/**
	 * 保存车辆所有数据 成功返回1
	 * 
	 * @param carid
	 * @return
	 */
	@RequestMapping(value = "addWCCARInfo", method = RequestMethod.POST)
	public int addWCCARInfo(WC_CAR_Info info) {
		info.setCreatedate(BaseTools.getCurrentTimeMillis());
		return carinfo.addWCCARInfo(info);
	}

	/********************************************************************************************
	 * 持久化数据到 wc_fk_custinfo 保存数据到custinfo
	 * 必须上传登录名
	 */
	@RequestMapping(value = "addWC_FK_CustInfo", method = RequestMethod.POST)
	public int addWC_FK_CustInfo(WC_FK_CustInfo custinfo) {

		/***
		 * user_login_name 必填项
		 */
		if (custinfo.getUser_login_name() == null || custinfo.getUser_login_name() == BaseTools.EMPTY) {

			return 0;

		}
		custinfo.setCreatedate(BaseTools.getCurrentTimeMillis());
		String username = custinfo.getUser_login_name().trim(); // 获取当前生成订单的用户名；
		User u = userService.sel(username);
		custinfo.setApplyusername(u.getUserName());
		custinfo.setByusername(u.getCode());// 设置归属的业务员
		return custInfoService.saveCustInfo(custinfo);
	}

	/***
	 * 获取所有wc_fk_custinfo 表数据
	 * 
	 * @param custinfo
	 * @return
	 */
	@RequestMapping(value = "getAllWC_FK_CustInfo", method = RequestMethod.GET)
	public List<WC_FK_CustInfo> getAllWC_FK_CustInfo() {

		return custInfoService.getAllWC_FK_CustInfo();
	}

	/***
	 * 获取所有wc_fk_custinfo 表数据
	 * 
	 * @param custinfo 根据orderid条件查询
	 * @return
	 */
	@RequestMapping(value = "getAllCustInfoForWhereLike", method = RequestMethod.GET)
	public List<WC_FK_CustInfo> getAllCustInfoForWhereLike(@RequestParam("orderid") String orderid) {

		return custInfoService.getAllCustInfoForWhereLike(orderid);
	}

	/****
	 * 订单表信息
	 * ******************************************************************************************
	 */
	/**
	 * 获取订单所有数据
	 * 
	 * @return
	 */
	@RequestMapping(value = "getAllWCComOrder", method = RequestMethod.GET)
	public List<WC_COM_Order> getAllWCComOrder() {

		return comorder.getAllWCComOrder();
	}
	
	/**
	 * 根据用户名username 和 forbyusername
	 * 获取 相关数据
	 * @param username
	 * @return
	 */
	@RequestMapping(value = "getWCComOrderForUserNameOrForbyusername", method = RequestMethod.GET)
	public List<WC_COM_Order> getWCComOrderForUserNameOrForbyusername(@RequestParam("username") String username,@RequestParam("forbyusername") String forbyusername) {
		return comorder.getWCComOrderForUserNameOrForbyusername(username,forbyusername);
	}
	
	
	
	/**
	 * 根据用户名username 获取相关所有数据
	 * 
	 * @param username
	 * @return
	 */
	@RequestMapping(value = "getWCComOrderForUserName", method = RequestMethod.GET)
	public List<WC_COM_Order> getWCComOrderForUserName(@RequestParam("username") String username) {

		return comorder.getWCComOrderForUserName(username);
	}
	

	/**
	 * 根据订单号ordernum 获取相关所有数据
	 * 
	 * @param ordernum
	 * @return
	 */
	@RequestMapping(value = "getWCComOrderForNum", method = RequestMethod.GET)
	public List<WC_COM_Order> getWCComOrderForNum(@RequestParam("ordernum") String ordernum) {

		return comorder.getWCComOrderForNum(ordernum);
	}
	
	
	/**
	 * 修改订单状态
	 * 
	 * @param order
	 * @return
	 */
	@RequestMapping(value = "upWCCOMOrderStatusForNum", method = RequestMethod.POST)
	public int upWCCOMOrderStatusForNum(String ordernum,String status) {
		
		
		return comorder.upWCCOMOrderStatusForNum(ordernum, status);
	}
	

	/**
	 * 保存订单表所有数据 成功返回1
	 * 
	 * @param order
	 * @return
	 */
	@RequestMapping(value = "addWCCOMOrder", method = RequestMethod.POST)
	public int addWCCOMOrder(WC_COM_Order order) {

		String username = order.getUsername().trim(); // 获取当前生成订单的用户名；
		User u1 = userService.sel(username);
		order.setForbyusername(u1.getCode());// 设置归属的业务员
		order.setCreatedata(BaseTools.getCurrentTimeMillis());
		order.setOrdernum(BaseTools.getRandomForOrderNum());
		order.setUsername(u1.getNickName());//充填客户的真实姓名；
		
		return comorder.addWCComOrder(order);
	}

}
